package com.example.sj6;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity4 extends AppCompatActivity {

    private static final int LOGIN_START = 1;
    private static final int LOGIN_SUCCESS = 2;
    private static final int LOGIN_FAILED = 3;

    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonLogin;
    private ProgressBar progressBar;
    private CheckBox cbAgree;
    private TextView tvRegister;

    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case LOGIN_START:
                    progressBar.setVisibility(View.VISIBLE);
                    buttonLogin.setEnabled(false);
                    Toast.makeText(MainActivity4.this, "正在登录...", Toast.LENGTH_SHORT).show();
                    break;
                case LOGIN_SUCCESS:
                    progressBar.setVisibility(View.GONE);
                    buttonLogin.setEnabled(true);
                    Toast.makeText(MainActivity4.this, "登录成功", Toast.LENGTH_SHORT).show();
                    break;
                case LOGIN_FAILED:
                    progressBar.setVisibility(View.GONE);
                    buttonLogin.setEnabled(true);
                    Toast.makeText(MainActivity4.this, "登录失败，请重试", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        initViews();
        setupListeners();
    }

    private void initViews() {
        editTextUsername = findViewById(R.id.editText_username);
        editTextPassword = findViewById(R.id.editText_password);
        buttonLogin = findViewById(R.id.button_login);
        progressBar = findViewById(R.id.progressBar);
        cbAgree = findViewById(R.id.cbAgree);
        tvRegister = findViewById(R.id.tvRegister);
    }

    private void setupListeners() {
        // 协议勾选框监听
        cbAgree.setOnCheckedChangeListener((buttonView, isChecked) -> {
            buttonLogin.setEnabled(isChecked);
            buttonLogin.setBackgroundTintList(getResources().getColorStateList(
                    isChecked ? android.R.color.holo_blue_light : android.R.color.darker_gray));
        });

        // 登录按钮监听
        buttonLogin.setOnClickListener(v -> {
            String username = editTextUsername.getText().toString().trim();
            String password = editTextPassword.getText().toString().trim();

            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                return;
            }

            simulateLoginProcess();
        });

        // 注册文本监听
        tvRegister.setOnClickListener(v -> {
            progressBar.setVisibility(View.VISIBLE);
            new Handler(Looper.getMainLooper()).postDelayed(() -> {
                progressBar.setVisibility(View.GONE);
                Toast.makeText(MainActivity4.this, "跳转到注册页", Toast.LENGTH_SHORT).show();
            }, 3000);
        });
    }

    private void simulateLoginProcess() {
        handler.sendEmptyMessage(LOGIN_START);

        new Thread(() -> {
            try {
                Thread.sleep(3000); // 模拟3秒加载过程

                // 模拟登录验证（这里简单判断）
                if (!editTextUsername.getText().toString().trim().isEmpty() &&
                        !editTextPassword.getText().toString().trim().isEmpty()) {
                    handler.sendEmptyMessage(LOGIN_SUCCESS);
                } else {
                    handler.sendEmptyMessage(LOGIN_FAILED);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                handler.sendEmptyMessage(LOGIN_FAILED);
            }
        }).start();
    }
}